<?php
require_once 'OAuth/OAuthException.php';
require_once 'OAuth/MathServer.php';

/**
 * Class RestController
 *
 * Description for class RestController
 *
 * @author: langxangvn
*/
class ApiMathController extends Zend_Controller_Action {
	/**
	 * This is method init
	 *
	 * @return mixed This is the return value description
	 *
	 */
	public function init() {
		/* Initialize action controller here */
	}
	
	/**
	 * indexAction
	 *
	 * @return void
	 */
	public function indexAction() {
		$this->_helper->Layout->disableLayout();		// disble layout
		$this->_helper->ViewRenderer->setNoRender();	// no renderer
				
		echo "Failed to validate oauth signature and token";
	}
	
	/**
	 * indexAction
	 *
	 * @return void
	 */
	public function addAction() {
		$this->handle('add');
	}
	
	/**
	 * indexAction
	 *
	 * @return void
	 */
	public function subtractAction() {
		$this->handle('subtract');
	}
	
	/**
	 * indexAction
	 *
	 * @return void
	 */
	public function multiplyAction() {
		$this->handle('multiply');
	}
	
	/**
	 * indexAction
	 *
	 * @return void
	 */
	public function divideAction() {
		$this->handle('divide');
	}
	
	/**
	 * *
	 *
	 * @param mixed $action This is a description
	 * @return mixed This is the return value description
	 *
	 */	
	protected function handle($action) {
		$this->_helper->Layout->disableLayout();		// disble layout
		$this->_helper->ViewRenderer->setNoRender();	// no renderer
		
		$params = $this->_request->getParams();
		unset($params['controller']);
		unset($params['action']);
		unset($params['module']);
		
		if (!array_key_exists(OAuthRequest::$API_FORMAT, $params)){
			$format = 'json';
		} else {
			$format = $params[OAuthRequest::$API_FORMAT];
		}		
		
		switch ($format) {
			case 'xml':;
				$this->_response->setHeader('Content-Type', 'text/xml');
				break;
			
			case 'plain-text':				
				$this->_response->setHeader('Content-Type', 'text/html');
				break;
			
			case 'json':			
				$this->_response->setHeader('Content-Type', 'application/json');
				break;
			
			default:
				$this->_response->setHeader('Content-Type', 'application/json');
		}	
		
		try {
			$server = new MathServer();
			$result = $server->handle($action, $params);
			
			switch ($format) {
				case 'xml':
					$response = OAuthResponse::generate(array('result' => $result));
					break;
				
				case 'plain-text':				
					$response = $result;
					break;
				
				case 'json':
					$response = Zend_Json::encode($result);	
					break;
				
				default:
					$format = 'json';
					$code = OAuthRequestVerifier::$UNSUPPORTED_RESPONSE_FORMAT;
					$error = $server->getErrorMessage($code);
					throw new OAuthException($error, $code);
			}			
			
			$this->_response->setBody($response);
		} catch(OAuthException $e) {
			$error = $server->fault($e, $format);
			$this->_response->setBody($error);
		} catch(Exception $e) {
			$error = $server->fault($e, $format);
			$this->_response->setBody($error);
			$this->_response->setRawHeader('HTTP/1.0 400 Bad Request');		
		}
	}
}
?>
